#include<iostream>
#include<map>
#include<string>
#include<algorithm>

using namespace std;

//map/multimap容器
//map中所有元素都是pair，pair中第一个元素为key(键值),起索引作用
// 第二个元素为vlaue(实值)， 所有元素都会根据元素的键值自动排序

//map/multimap属于关联式容器，底层结构是二叉树实现，可以根据key值快速找到value值
//map不允许容器中有重复key值元素，multimap允许容器中有重复key值元素



//map构造和赋值
void printMap(map<int, int>& m1)
{
	for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++)
	{
		cout << "key = " << (*it).first << "\tvalue = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<int, int> m1;
	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(3, 30));
	m1.insert(pair<int, int>(2, 20));
	m1.insert(pair<int, int>(4, 40));

	printMap(m1);

	//拷贝构造
	map<int, int> m2(m1);
	printMap(m1);

	//赋值
	map<int, int> m3;
	m3 = m2;
	printMap(m3);
}

int main()
{
	test01();
	return 0; 
}






//map容器大小和交换
//map.size()	返回容器中元素的数目
//map.empty()	判断容器是否为空
//map.swap(m1)	map和m1两个容器交换

void printMap(map<int, int>& m1)
{
	for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++)
	{
		cout << "key = " << (*it).first << "\tvalue = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<int, int> m1;
	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(3, 30));
	m1.insert(pair<int, int>(2, 20));
	m1.insert(pair<int, int>(4, 40));

	printMap(m1);

	if (m1.empty())
	{
		cout << "该容器为空。" << endl;
	}
	else
	{
		cout << "该容器不为空。" << endl;
		cout << "容器大小为: " << m1.size() << endl;
	}

	map<int, int> m2;
	m2.insert(pair<int, int>(5, 500));
	m2.insert(pair<int, int>(6, 600));
	m2.insert(pair<int, int>(7, 700));
	m2.insert(pair<int, int>(8, 800));
	
	m1.swap(m2);
	printMap(m1);
	printMap(m2);
}


int main()
{
	test01();
	return 0;
}






//map插入和删除
//map.insert(elem);		//在容器中插入元素
//map.emplace(elem...); //将创建新键值对所需的数据作为参数直接传入,map容器将直接构造元素
//map.clear();			//清除所有元素
//map.erase(pos);		//删除pos迭代器所指的元素，返回下一个元素的迭代器
//map.erase(beg, end);	//删除区间[beg, end)的所有元素，返回下一个元素的迭代器
//map.earse(key);		//删除容器中值为key的元素
void printMap(map<int, int>& m1)
{
	for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++)
	{
		cout << "key = " << (*it).first << "\tvalue = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<int, int> m1;
	//插入
	//第一种
	m1.insert(pair<int, int>(1, 10));
	//第二种
	m1.insert(make_pair(3, 30));
	//第三种
	m1.insert(map<int,int>::value_type(2, 20));
	//第四种
	m1[4] = 40;		
	//这是两步，首先m[4] 创建一个键值对，key=4， value默认为0，第二步赋值40

	printMap(m1);


	//[]不建议用于插入，[]更多用于利用key访问value
	cout << m1[2] << endl;

    //emplace
    m1.emplace(pair<int, int>(10, 100));    //同insert一样
    m1.emplace(11, 110);    //map容器直接构造元素

	//删除
	m1.erase(m1.begin());
	printMap(m1);

	m1.erase(3);	//按照key删除
	printMap(m1);

	//m1.erase(m1.begin(), m1.end());	//清空
	m1.clear();
	printMap(m1);	
}

int main()
{
	test01();
	return 0;
}








//map查找和统计
//map.find(key)		查找key是否存在，若存在，返回该元素的迭代器。
//					若不存在，返回map.end()
//map.count(key)	统计key的元素个数
void printMap(map<int, int>& m1)
{
	for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++)
	{
		cout << "key = " << (*it).first << "\tvalue = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<int, int> m1;
	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(3, 30));
	m1.insert(pair<int, int>(2, 20));
	m1.insert(pair<int, int>(4, 40));

	printMap(m1);

	map<int, int>::iterator pos = m1.find(3);
	if (pos != m1.end())
	{
		cout << "查找到了元素key = " << pos->first << "\tvalue = " << (*pos).second << endl;
	}
	else
	{
		cout << "未找到元素。" << endl;
	}

	cout << m1.count(3) << endl;
}

int main()
{
	test01();
	return 0;
}







//map容器排序
class MyCompare
{
public:
	bool operator() (int v1, int v2) const
	{
		return v1 > v2;
	}
};

void printMap(map<int, int, MyCompare>& m1)
{
	for (map<int, int, MyCompare>::iterator it = m1.begin(); it != m1.end(); it++)
	{
		cout << "key = " << (*it).first << "\tvalue = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<int, int, MyCompare> m1;

	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(3, 30));
	m1.insert(pair<int, int>(2, 20));
	m1.insert(pair<int, int>(4, 40));

	printMap(m1);

}

int main()
{
	test01();
	return 0;
}








//案例——员工分组
#include<map>
#include<string>
#include<vector>
#include<ctime>

#define CEHUA 0
#define MEISHU 1
#define YANFA 2

class Worker
{
public:

	string m_Name;
	int m_Salary;
};

void creatWorker(vector<Worker>& v)
{
	string nameSeed = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i++)
	{
		Worker worker;
		worker.m_Name = "员工";
		worker.m_Name += nameSeed[i];
	
		worker.m_Salary = rand() % 10000 + 10000;
		v.push_back(worker);
	}

}

void setGroup(vector<Worker>& vw, multimap<int, Worker>& mw)
{
	for (vector<Worker>::iterator it = vw.begin(); it != vw.end(); it++)
	{
		//产生随机部门编号
		int deptID = rand() % 3;

		//将员工插入到分组中
		mw.insert(make_pair(deptID, *it));
	}

}

void showWorker(multimap<int, Worker>& mw)
{
	cout << "策划部门: " << endl;
	multimap<int, Worker>::iterator pos = mw.find(CEHUA);
	int count = mw.count(CEHUA);
	int index = 0;
	for (; pos != mw.end() && index < count; pos++, index++)
	{
		cout << "姓名: " << pos->second.m_Name << "\t工资: " << pos->second.m_Salary << endl;

	}
	
	cout << "--------------------------------------" << endl;

	cout << "美术部门: " << endl;

	pos = mw.find(MEISHU);
	count = mw.count(MEISHU);
	index = 0;
	for (; pos != mw.end() && index < count; pos++, index++)
	{
		cout << "姓名: " << pos->second.m_Name << "\t工资: " << pos->second.m_Salary << endl;

	}

	cout << "--------------------------------------" << endl;

	cout << "研发部门: " << endl;

	pos = mw.find(YANFA);
	count = mw.count(YANFA);
	index = 0;
	for (; pos != mw.end() && index < count; pos++, index++)
	{
		cout << "姓名: " << pos->second.m_Name << "\t工资: " << pos->second.m_Salary << endl;

	}
	
}

int main()
{
	srand((unsigned int)time(NULL));
	vector<Worker> vw;
	//创建员工
	creatWorker(vw);
	
	//员工分组
	multimap<int, Worker> mw;
	setGroup(vw, mw);

	//显示员工
	showWorker(mw);

	return 0;
}

//哈希表的构造
umap();				//创建一个空的umap容器
umap(size_t bucket);	//创建一个空的umap容器，指定桶为bucket个
umap(initalizer_list<pair<K, V> > il);	//使用初始化列表
umap(initalizer_list<pair<K, V> > il, size_t bucket);	//使用初始化列表，定义桶的个数
umap(iteartor first, iterator last);	//用迭代器
umap(iteartor first, iterator last, size_t bucket);	//用迭代器
umap(const umap<K, V>& m);	//拷贝构造